// pages/broadcast/index/select-region/select-region.js
import {getStorageSync, setStorage} from "../../utils/Storage";
import {getRegions} from "../../utils/Location";
import Config from "../../main.config"
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        show: { //是否显示弹框
            type: Boolean,
            value: false
        },
        region: { //区域值
            type: String,
            value: "北京市,北京市,朝阳区"
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _regions: null,
        _regionPop: {
            loading: false,
            show: false,
            error: false,
            list: []
        },
    },

    /*
    * 监听
    * */
    observers: {
        "show": function (value) {
            if (value) {
                this.init();
            }
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {

        /*
        * 定位相关事件
        * */
        init: async function () { //点击cell
            try {
                let list = [];
                let regions = this.data._regions || getStorageSync(Config.storage.regions);
                this.setData({"_regionPop.show": true});
                if (regions) {
                    this.data._regions = regions;
                    list = this._getRegionList();
                } else {
                    this.setData({"_regionPop.loading": true})
                    regions = await getRegions();
                    this.data._regions = regions;
                    list = this._getRegionList();
                    setStorage(Config.storage.regions, regions);
                }
                this.setData({"_regionPop.list": list})
            } catch (e) {
                this.setData({"_regionPop.error": true});
                console.error(e)
            } finally {
                this.setData({"_regionPop.loading": false})
            }
        },

        /*
        * 获取区域列表信息
        * */
        _getRegionList: function (region) {
            let regions = this.data._regions;
            let currentRegion = region || this.data.region || "北京市,北京市,朝阳区";
            let fistList, fistInx, firstFullName, secList, secInx, thirdList, thirdInx;
            let currentArr = currentRegion ? currentRegion.split(",") : [];
            fistList = regions[0];
            fistInx = fistList.findIndex(function (item) {
                return item.fullname === currentArr[0]
            })
            firstFullName = fistList[fistInx].fullname;
            if (firstFullName.indexOf("市") !== -1) { //直辖市
                secList = [fistList[fistInx]];
                secInx = 0;
                thirdList = regions[1].slice(secList[secInx].cidx[0], secList[secInx].cidx[1] + 1);
                thirdInx = currentArr[2] ? thirdList.findIndex(function (item) {
                    return item.fullname === currentArr[2]
                }) : 0
            } else {
                secList = regions[1].slice(fistList[fistInx].cidx[0], fistList[fistInx].cidx[1] + 1);
                secInx = currentArr[1] ? secList.findIndex(function (item) {
                    return item.fullname === currentArr[1]
                }) : 0
                thirdList = regions[2].slice(secList[secInx].cidx[0], secList[secInx].cidx[1] + 1);
                thirdInx = currentArr[2] ? thirdList.findIndex(function (item) {
                    return item.fullname === currentArr[2]
                }) : 0
            }
            return [{
                values: fistList,
                defaultIndex: fistInx
            }, {
                values: secList,
                defaultIndex: secInx
            }, {
                values: thirdList,
                defaultIndex: thirdInx
            }]
        },

        /*
        * 单列变动
        * */
        onRegionChange: function (e) {
            let index = e.detail.index;
            let value = e.detail.value;
            let currentRegion = "";
            for (let i = 0; i <= index; i ++) {
                if (i == 0) {
                    currentRegion = value[i].fullname;
                } else {
                    currentRegion = (currentRegion + "," + value[i].fullname)
                }
            }
            let list = this._getRegionList(currentRegion);
            this.setData({"_regionPop.list": list})
        },

        /*
        * 区域选择组件确定按钮
        * */
        onRegionConfirm: function (e) {
            let valueLst = e.detail.value;
            let currentRegion = this.data.region
            let value = valueLst[0].fullname + "," + valueLst[1].fullname + "," + valueLst[2].fullname;
            currentRegion !== value ? this.triggerEvent("selectRegion", value, {}) : null;
            this.setData({"_regionPop.show": false})
        },

        /*
        * 关闭弹框
        * */
        onRegionClose: function () {
            this.triggerEvent("selectRegion", false, {})
            this.setData({"_regionPop.show": false})
        }
    }
})
